package com.jiang.books.domain;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;
import javax.validation.constraints.Size;

import static javax.persistence.GenerationType.SEQUENCE;

/*
Create table jiang_award
    (
		id             Number(20,0) NOT NULL ENABLE,
		award_name     Varchar2(100),
        CONSTRAINT pk_awards PRIMARY KEY (id),
		CONSTRAINT award_name_unique UNIQUE (award_name)
    );
*/

@Entity
@Table(name = "jiang_award")
public class Award implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private BigInteger id;
	private String awardName;
	private Set<Author> authors = new HashSet<Author>();
    private Set<Book> books = new HashSet<Book>();

	public Award() {
	}

	public Award(String awardName) {
		this.awardName = awardName;
	}

	public Award(String awardName, Set<Author> authors) {
		this.awardName = awardName;
		this.authors = authors;
	}

	@Id
    @GeneratedValue(strategy = SEQUENCE, generator="JIANG_SEQ")
    @SequenceGenerator(name="JIANG_SEQ", sequenceName="jiang_id_sequence", allocationSize=200)
    @Column(name = "ID", unique = true, nullable = false)
	public BigInteger getId() {
		return this.id;
	}

	public void setId(BigInteger id) {
		this.id = id;
	}

    @Column(name = "AWARD_NAME")
    @Size(min=1, max=100)
    public String getAwardName() {
        return this.awardName;
    }

    public void setAwardName(String awardName) {
        this.awardName = awardName;
    }


	@ManyToMany
	@JoinTable(name = "jiang_author_awards",
	           joinColumns = @JoinColumn(name = "AWARD_ID"),
	           inverseJoinColumns = @JoinColumn(name = "AUTHOR_ID"))
	public Set<Author> getAuthors() {
		return this.authors;
	}

	public void setAuthors(Set<Author> authors) {
		this.authors = authors;
	}

    @OneToMany
    @JoinTable(name = "jiang_author_awards",
               joinColumns = @JoinColumn(name = "AWARD_ID"),
               inverseJoinColumns = @JoinColumn(name = "BOOK_ID"))
    public Set<Book> getBooks() {
        return this.books;
    }

    public void setBooks(Set<Book> books) {
        this.books = books;
    }
    
    
	@Transient
	public boolean isNew() {
		if (id == null) {
			return true;
		} else {
			return false;
		}
	}

	public String toString() {
		return "Award :" + getId() + ", " + getAwardName();
	}
}